//
//  WeiDianDingDanViewController.m
//  EJLX
//
//  Created by adc on 15/11/15.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "WeiDianDingDanViewController.h"
#import "WeiXinDingDanTableViewCell.h"
#import "TwoWeiXinDingDanTableViewCell.h"
#import "ThireWeiDianDingDanTableViewCell.h"
#import "FourWeiDianDingDanTableViewCell.h"
#import "HttpsManager.h"
#import "weiXinDingDan.h"
#import "HomeVC.h"
#import "GMDCircleLoader.h"
#import "shouYinTaiViewController.h"
#import "ShangPinPingJiaViewController.h"
@interface WeiDianDingDanViewController ()

@end

@implementation WeiDianDingDanViewController


-(void)viewWillAppear:(BOOL)animated
{
    //self.tabBarController.tabBar.hidden=NO;
    //self.hidesBottomBarWhenPushed=NO;
    [HttpsManager requestGetWeiDianDingDan:_canShu type:@"3" Competion:^(weiXinDingDan *weiDian)
    {
        if (weiDian!=nil)
        {
            _weiXin=weiDian;
            
            _listAry=_weiXin.otherAry;
            __weak WeiDianDingDanViewController *weakSelf = self;
            [tabel1 addPullToRefreshWithActionHandler:^{
                NSLog(@"当进行下拉刷新执行该方法");
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            
            [tabel1 addInfiniteScrollingWithActionHandler:^{
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            [tabel1 reloadData];
            
            
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }];
//    [HttpsManager requestGetWeiDianDingDan:@"0" Competion:^(weiXinDingDan *weiDian)
//     {
//         
//         
//         
//         
//     }];

}
- (void)requestEnd{
    [tabel1.pullToRefreshView stopAnimating];
    
    [tabel1.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
        self.navigationController.navigationBarHidden=YES;
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [backBut setBackgroundImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UILabel*daoLabel=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-40, 7,80, 30)];
    daoLabel.text=@"我的订单";
    daoLabel.textAlignment=NSTextAlignmentCenter;
    daoLabel.textColor=[UIColor whiteColor];
    [view addSubview:daoLabel];

    smellScr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, WIDE,45)];
    
//    smellScr.pagingEnabled=YES;
//    smellScr.bounces=NO;
//     smellScr.delegate=self;
     NSArray*ary=[NSArray arrayWithObjects:@"待付款",@"待发货",@"待收货",@"待评价",@"已完成",@"已取消",@"待退款", nil];
    for (int i=0;i<7;i++)
    {
        bigBut=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/5*i, 0, WIDE/5, 40)];
       
        [bigBut setTitle:ary[i] forState:UIControlStateNormal];
        bigBut.font=[UIFont systemFontOfSize:12];
        [bigBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [bigBut setTitleColor:RGB_COLOR(0x00c49f) forState:UIControlStateSelected];
        [bigBut addTarget:self action:@selector(changeDay:) forControlEvents:UIControlEventTouchUpInside];
        bigBut.tag=i+1;
        [smellScr addSubview:bigBut];
        if (i==0)
        {
            bigBut.selected=YES;
            _select=bigBut.tag;
            
        }
        
    }
    smellScr.contentSize=CGSizeMake(WIDE/5*7, 45);
    smellScr.showsHorizontalScrollIndicator=YES;
    //smellScr.pagingEnabled=YES;
    smellScr.bounces=NO;
    [self.view addSubview:smellScr];

    tabel1=[[UITableView  alloc]initWithFrame:CGRectMake(0,64+50,WIDE,HEIGHT-64-40-60) style:UITableViewStylePlain];
    tabel1.delegate=self;
    tabel1.dataSource=self;
    tabel1.showsHorizontalScrollIndicator=NO;
    tabel1.showsVerticalScrollIndicator =NO;
  
    tabel1.tableFooterView=[[UIView alloc]init];
    [self.view addSubview:tabel1];
    label=[[UILabel alloc]initWithFrame:CGRectMake(0,40,WIDE/5, 5)];
    label.backgroundColor=RGB_COLOR(0x00c49f);
    [smellScr addSubview:label];
   
    [tabel1 registerNib:[UINib nibWithNibName:@"WeiXinDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [tabel1 registerNib:[UINib nibWithNibName:@"TwoWeiXinDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    [tabel1 registerNib:[UINib nibWithNibName:@"ThireWeiDianDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell3"];
    [tabel1 registerNib:[UINib nibWithNibName:@"FourWeiDianDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell4"];

  
}
-(void)changeDay:(UIButton*)sender
{
    UIButton*lastBut=(UIButton*)[smellScr viewWithTag:_select];
    lastBut.selected=NO;
    sender.selected=YES;
    _select=sender.tag;
    [UIView animateWithDuration:1 animations:^{
        label.frame=CGRectMake(WIDE /5*(sender.tag-1),40,WIDE/5, 5);
    } completion:^(BOOL finished) {
        nil;
    }];
    //smellScr.contentOffset
    NSString*str=[NSString stringWithFormat:@"%ld",sender.tag];
    [HttpsManager requestGetWeiDianDingDan:str type:@"3" Competion:^(weiXinDingDan *weiDian)
     {
         if (weiDian!=nil)
         {
             _weiXin=weiDian;
             
             _listAry=_weiXin.otherAry;
             [tabel1 reloadData];
             
             
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             vc.hidesBottomBarWhenPushed=YES;
             [self.navigationController pushViewController:vc animated:YES];
         }
         
     }];

//    [HttpsManager requestGetWeiDianDingDan:str Competion:^(weiXinDingDan *weiDian)
//     {
//         if (weiDian!=nil)
//         {
//             _weiXin=weiDian;
//             
//             _listAry=_weiXin.otherAry;
//             [tabel1 reloadData];
//             
//             
//         }
//         else
//         {
//             LoginVC*vc=[[LoginVC alloc]init];
//             vc.hidesBottomBarWhenPushed=YES;
//             [self.navigationController pushViewController:vc animated:YES];
//         }
//         
//         
//         
//         
//         
//         
//         
//     }];
  

    
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
//{
//    //    NSLog(@" scrollViewDidScroll");
//  [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+WIDE/5*(_select-1), 0)];
//}

//-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    //int index=smellScr.contentOffset.x/;
//   // _control.selectedSegmentIndex=index;
//    [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+WIDE/5*(_select-1), 0)];
//   
//}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    twoWeiXinDingDan*zz=_listAry[section];
    
    return  zz.itemlistAry.count+3;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _listAry.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    twoWeiXinDingDan*zz=_listAry[indexPath.section];
    _itemlistAry=zz.itemlistAry;
   
    if (indexPath.row==0)
    {
        return 44;
    }
    if (0<indexPath.row&&indexPath.row<_itemlistAry.count+1)
    {
        return 120;
    }
    if (indexPath.row==_itemlistAry.count+1)
    {
        return 70;
    }
        return 44;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
   twoWeiXinDingDan*wei1=_listAry[indexPath.section];
    _itemlistAry=wei1.itemlistAry;
    //for (int i=0;i<_itemlistAry.count; i++)
   // {
    
    
    
     NSInteger b=a;
    NSLog(@"++++%ld",(long)b);
  
//    if (tableView==tabel1)
//    {
        if (indexPath.row==0)
        {
            NSArray*ary=[NSArray arrayWithObjects:@"待付款",@"待送货",@"待确认",@"待评价",@"已完成",@"已取消",@"待退款", nil];
            
            
            WeiXinDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
            if (wei1.type==1)
            {
                cell.maiJia.text=@"买家";
                cell.name.text=wei1.buyerName;
            }
            else{
                cell.maiJia.text=@"卖家";
                cell.name.text=wei1.venderName;
            }
            cell.last.text=ary[_select-1];
            cell.dianHua.tag=indexPath.section;
            [cell.dianHua addTarget:self action:@selector(dianHuaBut:) forControlEvents:UIControlEventTouchUpInside];
           // cell.name.text=weiXin.
            return cell;
        }
    else if (0<indexPath.row && indexPath.row<=_itemlistAry.count)
        {
            NSLog(@"rowNum = %ld, count = %ld",indexPath.row,_itemlistAry.count);
              thireWeiXinDingDan*wei3=_itemlistAry[indexPath.row-1];
            TwoWeiXinDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
           
            [cell.image sd_setImageWithURL:[NSURL URLWithString:wei3.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
            cell.name.text=wei3.name;
            
            cell.shouJia.text=[NSString stringWithFormat:@"%.2f",wei3.price*0.01];
           
            cell.guiGe.text=[NSString stringWithFormat:@"%d",wei3.quantity];
         
            return cell;
        
        }else if (indexPath.row==_itemlistAry.count+1)
        {
            ThireWeiDianDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
          
            cell.shiFu.text=[NSString stringWithFormat:@"%.2f",wei1.money*0.01];
          
            cell.jiJian.text=[NSString stringWithFormat:@"共%d",wei1.itemNum];
           
            cell.yunFei.text=[NSString stringWithFormat:@"%.2f",wei1.deliveryFee*0.01];
         
            return cell;
        }else{
    
               FourWeiDianDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell4" forIndexPath:indexPath];
            cell.fuKuan.layer.cornerRadius=5;
            cell.fuKuan.clipsToBounds=YES;
            cell.but1.layer.cornerRadius=5;
            cell.but1.clipsToBounds=YES;
            cell.but2.layer.cornerRadius=5;
            cell.but2.clipsToBounds=YES;
              //NSString*str2=[NSString stringWithFormat:@"%d",_weiXin.deliveryFee];
             cell.fuKuan.backgroundColor=RGB_COLOR(0x00c49f);
            [cell.fuKuan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
           
            
            switch (_select)
            {
                case 1:
                {
                    if (wei1.type==1)
                    {
                        [cell.fuKuan addTarget:self action:@selector(fuKuanButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                        [cell.fuKuan setTitle:@"付款" forState:UIControlStateNormal];
                        [cell.but1 setTitle:@"取消" forState:UIControlStateNormal];
                        cell.but1.backgroundColor=RGB_COLOR(0x00c49f);
                       // [cell.but1 setBackgroundColor:RGB_COLOR(0x00c49f)];
                        cell.but1.tag=indexPath.section;
                        [cell.but1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                        [cell.but1 addTarget:self action:@selector(quXiaoButClick:) forControlEvents:UIControlEventTouchUpInside];
                        
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];

                    }
                    else
                    {
                        [cell.fuKuan addTarget:self action:@selector(fuKuanButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                        [cell.fuKuan setTitle:@"付款" forState:UIControlStateNormal];
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];

                    }
                    
                }
                    break;
                case 2:
                {
                    if (wei1.type==1)
                    {
                        [cell.fuKuan setTitle:@"申请退款" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        cell.but1.backgroundColor=nil;

                        [cell.fuKuan addTarget:self action:@selector(faHuoButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                        
                    }
                    else{
                         [cell.fuKuan setTitle:@"申请退款" forState:UIControlStateNormal];
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                         cell.but1.backgroundColor=nil;
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.fuKuan addTarget:self action:@selector(faHuoButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                    }
                    
                }
                    break;
                case 3:
                {
                    if (wei1.type==1)
                    {
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.fuKuan setTitle:@"确认收货" forState:UIControlStateNormal];
                         cell.but1.backgroundColor=nil;
                        [cell.fuKuan addTarget:self action:@selector(shouHuoButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                        
                    }
                    else
                    {
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.fuKuan setTitle:@"关闭订单" forState:UIControlStateNormal];
                         cell.but1.backgroundColor=nil;
                        [cell.fuKuan addTarget:self action:@selector(shouHuoButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                    }
                }
                    break;
                case 4:
                {
                    [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                    [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                    [cell.fuKuan setTitle:@"评价" forState:UIControlStateNormal];
                     cell.but1.backgroundColor=nil;
                    [cell.fuKuan addTarget:self action:@selector(pingJiaButClick:) forControlEvents:UIControlEventTouchUpInside];
                    cell.fuKuan.tag=indexPath.section;
                }
                    break;
                case 5:
                {
                    
                }
                    break;
                case 6:
                {
                    [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                    [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                    [cell.fuKuan setTitle:@"查看" forState:UIControlStateNormal];
                     cell.but1.backgroundColor=nil;
                    [cell.fuKuan addTarget:self action:@selector(chaKanButClick:) forControlEvents:UIControlEventTouchUpInside];
                    cell.fuKuan.tag=indexPath.section;
                }
                    break;
                case 7:
                {
                    if (wei1.type==1)
                    {
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.fuKuan setTitle:@"" forState:UIControlStateNormal];
                         cell.but1.backgroundColor=nil;
                        [cell.fuKuan addTarget:self action:@selector(tuiKuanButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                    }
                    else
                    {
                        [cell.but1 setTitle:@"" forState:UIControlStateNormal];
                        [cell.but2 setTitle:@"" forState:UIControlStateNormal];
                        [cell.fuKuan setTitle:@"退款" forState:UIControlStateNormal];
                         cell.but1.backgroundColor=nil;
                        [cell.fuKuan addTarget:self action:@selector(tuiKuanButClick:) forControlEvents:UIControlEventTouchUpInside];
                        cell.fuKuan.tag=indexPath.section;
                    }
                }
                    break;
                    
                default:
                    break;
            }
    
           // [GMDCircleLoader hideFromView:tabel1 animated:YES];
            
               return cell;
        }
    
    }
-(void)dianHuaBut:(UIButton*)sender
{
    
    twoWeiXinDingDan*wei1=_listAry[sender.tag];
    if (wei1.type==1)
    {
        NSString*num=[[NSString alloc]initWithFormat:@"telprompt://%@",wei1.buyerPhone];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:num]];
    }
    else
    {
        NSString*num=[[NSString alloc]initWithFormat:@"telprompt://%@",wei1.venderPhone];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:num]];
    }
   
    
    
}
-(void)faHuoButClick:(UIButton*)sender
{
    if (sender.tag==0)
    {
        NSLog(@"11");
    }
}
-(void)shouHuoButClick:(UIButton*)sender
{
    
}
-(void)pingJiaButClick:(UIButton*)sender
{
    twoWeiXinDingDan*wei1=_listAry[sender.tag];
   // NSString*orderId=[NSString stringWithFormat:@"%d",wei1.orderId];
    // self.hidesBottomBarWhenPushed=YES;
    ShangPinPingJiaViewController*vc=[[ShangPinPingJiaViewController alloc]init];
    self.tabBarController.tabBar.hidden=YES;
    vc.orderId=[NSString stringWithFormat:@"%d",wei1.orderId];

    [self.navigationController pushViewController:vc animated:YES];
     //self.hidesBottomBarWhenPushed=NO;
}
-(void)quXiaoButClick:(UIButton*)sender
{
    twoWeiXinDingDan*wei1=_listAry[sender.tag];
    NSString*orderId=[NSString stringWithFormat:@"%d",wei1.orderId];
    [HttpsManager requestQuXiaoDingDanCompletion:orderId completion:^(NSString *errmsg, int errcode)
     {
         SHOW_ALERT(errmsg);
         if (errcode==0)
         {
            // UITableViewCell *cell = (UITableViewCell*)[[[sender superview]superview] superview];
             //NSIndexPath *index = [tabel1 indexPathForCell:cell];
             [_listAry removeObjectAtIndex:sender.tag];
             [tabel1 reloadData];
         }
     }];
}
-(void)tuiKuanButClick:(UIButton*)sender
{
   // if (sender.tag==0)
    //{
        NSLog(@"11");
    //}
}
-(void)fuKuanButClick:(UIButton*)sender
{
//    switch (_select)
//    {
//        case 1:
//        {
            twoWeiXinDingDan*wei1=_listAry[sender.tag];
            _itemlistAry=wei1.itemlistAry;
            
            
            NSMutableString*order=[[NSMutableString alloc]init];
            [order appendString:[NSString stringWithFormat:@"%d",wei1.orderId]];
            
            shouYinTaiViewController*vc=[[shouYinTaiViewController alloc]init];
            vc.mony=[NSString stringWithFormat:@"%.2f",wei1.money*0.01];
            
            
            vc.o2o=@"false";
            vc.orderId=order;
            self.tabBarController.tabBar.hidden=YES;
            self.hidesBottomBarWhenPushed=YES;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//            break;
//        case 2:
//        {
//            
//        }
//            break;
//        case 3:
//        {
//            
//        }
//            break;
//        case 4:
//        {
    
//        }
//            break;
//        case 5:
//        {
//            
//        }
//            break;
//        case 6:
//        {
//            
//        }
//            break;
//        case 7:
//        {
//            
//        }
//            break;
//        default:
//            break;
//    }
    
    
    
}
//返回按钮
-(void)backBut
{
    HomeVC*vc=[[HomeVC alloc]init];
    self.tabBarController.tabBar.hidden=YES;
    self.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    //[self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)daiFuKuan
{
    
}
-(void)shiBaiBut
{
    
}
-(void)guanBiBut
{
    
}

    // Do any additional setup after loading the view from its nib.


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
